#include "queue.h"

queue *createQueue() {
    queue *q = malloc(sizeof(queue));
    checkAddress(q);
    q->insert = 0;
    q->pop = 0;
    return q;
}

void checkAddress(queue *q) {
    if (q == 0) {
        printf("Unable to allocate memory.\n");
        exit(EXIT_FAILURE);
    }
}

bool isFull(queue *q) {
    return q->pop == (q->insert + 1) % LENGTH;
}

void enqueue(queue *q, int value) {
    if (isFull(q)) {
        printf("Queue is full.");
        exit(EXIT_FAILURE);
    }
    q->data[q->insert] = value;
    q->insert = (q->insert + 1) % LENGTH;
}

void print_debug(queue *q) {
    for (int i = q->pop; i != q->insert; i = (i + 1) % LENGTH) {
        printf("%d -> ",q->data[i]);
    }
    printf("\n");
}

bool isEmpty(queue *q) {
    return q->pop == q->insert;
}

int dequeue(queue *q) {
    if (isEmpty(q)) {
        printf("Queue is empty.");
        exit(EXIT_FAILURE);
    }
    int ret = q->data[q->pop];
    q->pop = (q->pop + 1) % LENGTH;
    return ret;
}
